package com.open.capacity.workflow.service.impl;


import com.open.capacity.commons.Result;
import com.open.capacity.reimburse.util.uuid.UUIDUtils;
import com.open.capacity.workflow.dao.WFTaskHistoryDao;

import com.open.capacity.workflow.model.WFTaskHistory;
import com.open.capacity.workflow.service.WFTaskHistoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class WFTaskHistoryServiceImpl implements WFTaskHistoryService {

    @Autowired
    private WFTaskHistoryDao taskDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(WFTaskHistory taskHistory) {

        taskDao.save(taskHistory);
        log.info("保存流程历史：{}", taskHistory);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        WFTaskHistory taskHistory = taskDao.findById(id);

        taskDao.delete(id);

        log.info("删除流程历史：{}", taskHistory);
    }


    @Override
    public WFTaskHistory findById(String id) {
        return taskDao.findById(id);
    }

    @Override
    public List<WFTaskHistory> findTasksByProcessId(String processId) {
        Map<String, Object> params = new HashMap<>();
        params.put("processId", processId);
        List<WFTaskHistory> list = taskDao.findList(params);
        return list;
    }

    @Override
    public List<WFTaskHistory> findTaskHistorys(Map<String, Object> params) {
//        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
//        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
//            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
        List<WFTaskHistory> list = taskDao.findList(params);
        return list;
    }


    @Override
    public Result saveOrUpdate(WFTaskHistory taskHistory, String keyValue) {
        int i = 0;

        taskHistory.setId(UUIDUtils.getOriginalUUIDWithoutBar());
        i = taskDao.save(taskHistory);

        return i > 0 ? Result.succeed("操作成功") : Result.failed("操作失败");
    }


}
